Skip to content

gh-114782: Fix http.client splitting header lines on a bare CR - #162397

Open
star7js wants to merge 1 commit into
python:mainfrom
star7js:gh-114782-bare-cr
Open

star7js wants to merge 1 commit into
python:mainfrom
star7js:gh-114782-bare-cr

Conversation

@star7js

@star7js star7js commented Nov 21, 2026 •

Copy link
Copy Markdown

The email parser used by http.client.parse_headers() treats a bare CR as a line terminator, so one field line can be parsed as several header fields. With the request from the issue,

Visible: :/\rSmuggled: :)\r\n

is parsed as two headers, Visible and Smuggled. This affects http.client, http.server and wsgiref.simple_server.

RFC 9112 section 2.2 says a recipient of a bare CR must either treat the element as invalid or replace each bare CR with SP before processing. This change replaces it with SP in _read_headers(), so the line stays one field. I chose replacement over rejection because it cannot break clients that talk to servers sending a stray CR. Happy to switch to rejecting (at least in http.server) if that is preferred.

Only CR is affected: I checked the other characters str.splitlines() splits on (\x0b, \x0c, \x1c to \x1e, \x85) and none of them split a header line.

Tests: two in test_httplib and one in test_httpservers; all fail without the change.

The email parser used by http.client.parse_headers() treats a bare CR as a
line terminator, so one field line could be parsed as several header
fields. Replace a bare CR with SP before parsing, as RFC 9112 section 2.2
permits. This also covers http.server and wsgiref.simple_server.
@star7js
star7js requested a review from orsenthil as a code owner November 21, 2026 18:35
@python-cla-bot

python-cla-bot Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant